namespace taurus.client {

    /**
     * TArray entity.
     */
    export class TArray implements ITArray {
        private dataHolder: TDataWrapper[];

        public static newInstance(): ITArray {
            return new TArray();
        }
        public constructor() {
            this.dataHolder = [];
        }

        public static newFromBinaryData(bytes: ByteArray): ITArray {
            return TDataSerializer.me().binary2array(bytes);
        }

        public static newFromJson(json: string): ITArray {
            return TDataSerializer.me().json2array(json);
        }

        getBool(index: number): boolean {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getByte(index: number): number {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getByteArray(index: number): Uint8Array {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        get(index: number): TDataWrapper {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper;
        }

        getDouble(index: number): number {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getFloat(index: number): number {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getInt(index: number): number {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getLong(index: number): number {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getTArray(index: number): ITArray {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getTObject(index: number): ITObject {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getShort(index: number): number {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        getString(index: number): string {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return null;
            }
            return wrapper.data;
        }

        isNull(index: number): boolean {
            var wrapper = this.dataHolder[index];
            if (wrapper == null) {
                return false;
            }
            return wrapper.type == TDataType.NULL;
        }

        private addObject(value: any, typeId: TDataType) {
            this.dataHolder.push(new TDataWrapper(typeId, value));
        }

        add(val: TDataWrapper) {
            this.dataHolder.push(val);
        }

        addBool(val: boolean) {
            this.addObject(val, TDataType.BOOL);
        }

        addByte(val: number) {
            this.addObject(val, TDataType.BYTE);
        }

        addByteArray(val: Uint8Array) {
            this.addObject(val, TDataType.BYTE_ARRAY);
        }

        addDouble(val: number) {
            this.addObject(val, TDataType.DOUBLE);
        }

        addFloat(val: number) {
            this.addObject(val, TDataType.FLOAT);
        }

        addInt(val: number) {
            this.addObject(val, TDataType.INT);
        }

        addLong(val: number) {
            this.addObject(val, TDataType.LONG);
        }

        addNull() {
            this.dataHolder.push(new TDataWrapper(TDataType.NULL, null));
        }

        addTArray(val: ITArray) {
            this.addObject(val, TDataType.TARRAY);
        }

        addTObject(val: ITObject) {
            this.addObject(val, TDataType.TOBJECT);
        }

        addShort(val: number) {
            this.addObject(val, TDataType.SHORT);
        }

        addString(val: string) {
            this.addObject(val, TDataType.STRING);
        }

        size(): number {
            return this.dataHolder.length;
        }

        toBinary(): ByteArray {
            return TDataSerializer.me().array2binary(this);
        }

        toJson(): string {
            return TDataSerializer.me().array2json(this);
        }

    }

    /**
     * 
     */
    export class TArrayLite extends TArray {

        public constructor() {
            super();
        }

        public getByte(index: number): number {
            var d = super.getDouble(index);
            return d;
        }

        public getShort(index: number): number {
            var d = super.getDouble(index);
            return d;
        }

        public getFloat(index: number): number {
            var d = super.getDouble(index);
            return d;
        }

        public getLong(index: number): number {
            var d = super.getDouble(index);
            return d;
        }

        public getInt(index: number): number {
            var d = super.getDouble(index);
            return d;
        }
    }
}